﻿var SORTABLE = true;
var DEFAULTSORT = true;
var cfg = {
	title: "Title",
	url: "url",
	dataType: "xml",
	singleSelect: true,
	pageNo: 0,
	pageSize: 15,
	buttons:[
		["Text", "className", function(cmd, grid){}],
	],
	colModel: [
		["fieldName", "displayName", 100, SORTABLE, !DEFAULTSORT, "center", function(div, pid, rowData){}],
	]
};
var FIELD_NAMES = ["display", "name", "width", "sortable", "defaultsort", "align", "process"];
var DEFAULT_VALUES = ["DEMO", "demo", 100, true, "asc", "left", null];
function getFlexRowData(prnt){
	var data = $(prnt).get()[0].data.cell;
	var obj = {_indexes:[],_data:data};
	$("td", prnt).each(function(i, td){
		td = $(td);
		var name=td.attr("abbr");
		if(!!name){
			var value = td.text();
			obj[name] = value;
			obj._indexes.push(name);
		}
	});
	return obj;
}
function generateSimpleFlexGridCfg(cfg){
	var config = {};
	config.singleSelect = !!cfg.singleSelect;
	config.onRowClick = cfg.rowClick||null;
	config.onRowDbClick = cfg.rowDblClick||null;
	config.colModel = [];
	config.height = cfg.height||"auto";
	config.width = cfg.width||"auto";
	for(var i=0; i<cfg.colModel.length; i++){
		var colArr = cfg.colModel[i];
		var col = {};
		for(var j=0; j<DEFAULT_VALUES.length; j++){
			if(j==6){//render
				if(!!colArr[j]&&typeof colArr[j]=="function"){
					//alert(["render["+j+"]",colArr[j]].join("\n"));
					col[FIELD_NAMES[j]] = (function(){
						var colarr = colArr;
						var index = i;
						return function(div, pid, prnt){
							var rowData = getFlexRowData(prnt);
							//var name = rowData.indexes[index];
							var data = rowData._data[index];
							var inner = colarr[6](data, pid, rowData._data);
							rowData = null;
							$(div).html(inner);
						}
					})();
				}
			}else{//properties
				if(colArr[j]!=null){
					col[FIELD_NAMES[j]] = colArr[j];
				}else{
					col[FIELD_NAMES[j]] = DEFAULT_VALUES[j];
				}
			}
		}
		config.colModel.push(col);
	}
	return config;
}
function generateFlexGridCfg(cfg){
	var config = {usepager: true, useRp: true};
	config.height = cfg.height||390;
	config.title = cfg.title||"表格";
	config.url = cfg.url||"";
	config.dataType = cfg.dataType||"json";
	config.rp = cfg.pageSize||15;
	config.showTableToggleBtn = !!cfg.showButton||true;
	config.singleSelect = !!cfg.singleSelect;
	config.onRowClick = cfg.rowClick||null;
	config.onRowDbClick = cfg.rowDblClick||null;
	config.query = cfg.query||"";
	config.qtype = cfg.queryType||"";
	config.preProcess = function(data){
	    if(!!data&&!!(data.total)){
	        var rows = data.rows;
	        $.each(rows, function(i, row){
	            $.each(row.cell, function(i, c){
	                if((!!c)&&(typeof c==="string")){
	                    row.cell[i] = c.replace(/</g, "&lt;").replace(/>/g, "&gt;");
	                } 
	            });
	        });
	    }
	    return data;
	  };
	var default_sort_name = cfg.colModel[0][1];
	var default_sort_order = cfg.colModel[0][4]||"asc";
	config.colModel = [];
	for(var i=0; i<cfg.colModel.length; i++){
		var colArr = cfg.colModel[i];
		var col = {};
		for(var j=0; j<DEFAULT_VALUES.length; j++){
			if(j==4){//default-sort
				if(!!colArr[j]){
					default_sort_name = colArr[1];
					default_sort_order = colArr[j];
				}
			}else
			if(j==6){//render
				if(!!colArr[j]&&typeof colArr[j]=="function"){
					//alert(["render["+j+"]",colArr[j]].join("\n"));
					col[FIELD_NAMES[j]] = (function(){
						var colarr = colArr;
						var index = i;
						return function(div, pid, prnt){
							var rowData = getFlexRowData(prnt);
							//var name = rowData.indexes[index];
							var data = rowData._data[index];
							var inner = colarr[6](data, pid, rowData._data);
							rowData = null;
							$(div).html(inner);
						}
					})();
				}
			}else{//properties
				if(colArr[j]!=null){
					col[FIELD_NAMES[j]] = colArr[j];
				}else{
					col[FIELD_NAMES[j]] = DEFAULT_VALUES[j];
				}
			}
		}
		config.colModel.push(col);
	}
	config.sortname = default_sort_name;
	config.sortorder = default_sort_order;
	if(cfg.buttons&&cfg.buttons.length){
		var buttons = [];
		var bts = cfg.buttons;
		for(var i=0; i<bts.length; i++){
			if(!bts[i]){
				continue;
			}
			var bt = {};
			if(!bts[i][0]||!bts[i][2]){
				bt.separator = true;
			}else{
				bt.name = bts[i][0];
				bt.bclass = bts[i][1];
				bt.onpress = (function(){
					var func = bts[i][2];
					return function(id, div){
						if(!+id){
							id=0;
						}
						func(id);
					};
				})();
			}
			buttons.push(bt);
		}
		if(buttons.length>0){
			config.buttons = buttons;
		}
	}
	return config;
}